package com.hnqcgczy.shop.dao.impl;

import com.hnqcgczy.shop.dao.IUserDAO;
import com.hnqcgczy.shop.db.MyTransaction;
import com.hnqcgczy.shop.vo.TUser;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDAOImpl implements IUserDAO {
    @Override
    public boolean login(String username, String pwd) throws SQLException {
        MyTransaction f=new MyTransaction(true);
        Connection conn= f.getConn();
        PreparedStatement preparedStatement = conn.prepareStatement("select  *from t_user where tu_id=? and tu_password=?");
        preparedStatement.setString(1,username);
        preparedStatement.setString(2,pwd);
        ResultSet resultSet=preparedStatement.executeQuery();
        boolean flag=false;
        while (resultSet.next()){
            flag=true;
        }
        if (resultSet!=null){
            resultSet.close();
        }
        if (preparedStatement!=null){
            preparedStatement.close();
        }
        return flag;
    }


    @Override
    public boolean register(TUser tUser) throws SQLException {
        MyTransaction myTransaction = new MyTransaction(true);
        Connection conn = myTransaction.getConn();
        PreparedStatement ps = conn.prepareStatement("insert into t_user(tu_id,tu_password)values (?,?)");
        ps.setString(1,tUser.getTuId());
        ps.setString(2,tUser.getTuPassword());
        int i=ps.executeUpdate();
        if (ps!=null){
            ps.close();
        }
        return i>0;
    }

    public static void main(String[] args) {
        TUser user=new TUser();
        user.setTuId("s");
        user.setTuPassword("123456");
        try {
            // boolean b = new UserDAOImpl().register(user);
           boolean b=new UserDAOImpl().login("s","123456");
            System.out.println(b);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }
    }



